package com.runmi.webchat.aftersale.web.note.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.runmi.webchat.aftersale.common.utils.BaseExcelUtil;
import com.runmi.webchat.aftersale.common.utils.DateUtil;
import com.runmi.webchat.aftersale.common.utils.DownLoadFileUtil;
import com.runmi.webchat.aftersale.common.utils.NumberUtil;
import com.runmi.webchat.aftersale.common.utils.StringUtil;
import com.runmi.webchat.aftersale.dbcore.dao.ShopInfoDao;
import com.runmi.webchat.aftersale.dbcore.entity.ShopInfo;
import com.runmi.webchat.aftersale.dbcore.entity.User;
import com.runmi.webchat.aftersale.dbcore.entity.UserNote;
import com.runmi.webchat.aftersale.dbcore.mapper.SetupItemMapper;
import com.runmi.webchat.aftersale.dbcore.mapper.UserNoteMapper;
import com.runmi.webchat.aftersale.util.Constants;
import com.runmi.webchat.aftersale.web.base.BaseService;
import com.runmi.webchat.aftersale.web.note.vo.NoteIndexReqVo;
import com.runmi.webchat.aftersale.web.setup.vo.SetupItemReqVo;
import com.runmi.webchat.aftersale.web.setup.vo.SetupOrderResVo;

@Service
public class NoteService extends BaseService{

	@Autowired
	private  SetupItemMapper setupItemMapper;
	
	@Autowired
	private ShopInfoDao shopInfoDao;
	
	@Autowired
	private UserNoteMapper userNoteMapper;
	
	
	public void index(NoteIndexReqVo model){
		 try{		
			 
			    model.setShopInfos(shopInfoDao.getAllShopInfoList());		 
			    PageHelper.startPage(model.getCpage(), model.getPageSize());
				List<Map> items=userNoteMapper.getUserNoteInfoByContion(
						model.getShopId(), 0,
						model.getStatus(),
						model.getCustomerName(), model.getMobile());
					
				PageInfo<Map> pageInfo = new PageInfo<Map>(items);
				model.setTotalCount((int)pageInfo.getTotal());
				
				 for(Map item:items){
		            	//Long salePrice=null==item.get("sale_price")?null:Long.parseLong(item.get("sale_price").toString());
		            	//item.put("price",NumberUtil.forPriceView(salePrice));
		            	
		            }
				
				
				model.setItems(items);
			}catch(Exception ex){
				ex.printStackTrace();
				
			}
	}
	
	 public void exportXls(
	    		HttpServletRequest request, 
				HttpServletResponse response,
				NoteIndexReqVo model){
	        try{
	        	
				String fileName ="需求导出";

				String[] titleName = {"门店编码","门店名称","客户姓名","客户手机","感兴趣商品","服务要求", "状态 ", "创建时间","处理意见"};			
				SXSSFSheet sheet = BaseExcelUtil.templateBuild(titleName, fileName);
		        CellStyle style = BaseExcelUtil.createGeneralCellStyle(sheet.getWorkbook());
		        int rowNum = 1;

		        List<Map> items=userNoteMapper.getUserNoteInfoByContion(
						model.getShopId(), 0,
						model.getStatus(),
						model.getCustomerName(), model.getMobile());
	            for(Map item:items){

	            	String status=item.get("status").toString();
	            	String statusName="";
	            	if(status.equals("0")){
	            		statusName="未处理 ";
	            	}
	            	if(status.equals("1")){
	            		statusName="已完成 ";
	            	}
	            	
	            	Date market_time = (Date)item.get("create_time");
					String market_timeStr = "";
					if(null != market_time){
						market_timeStr = DateUtil.format(market_time,"yyyy/MM/dd HH:mm:ss");
					}				

					BaseExcelUtil.cellBuild(rowNum, 0, sheet, style, Cell.CELL_TYPE_STRING, item.get("shop_code"));
	            	BaseExcelUtil.cellBuild(rowNum, 1, sheet, style, Cell.CELL_TYPE_STRING, item.get("shop_name"));
	            	BaseExcelUtil.cellBuild(rowNum, 2, sheet, style, Cell.CELL_TYPE_STRING, item.get("customer_name"));
					BaseExcelUtil.cellBuild(rowNum, 3, sheet, style, Cell.CELL_TYPE_STRING,item.get("mobile") );
					BaseExcelUtil.cellBuild(rowNum, 4, sheet, style, Cell.CELL_TYPE_STRING, item.get("item_name"));
					BaseExcelUtil.cellBuild(rowNum, 5, sheet, style, Cell.CELL_TYPE_STRING, item.get("remark"));
					BaseExcelUtil.cellBuild(rowNum, 6, sheet, style, Cell.CELL_TYPE_STRING, statusName);
					BaseExcelUtil.cellBuild(rowNum, 7, sheet, style, Cell.CELL_TYPE_STRING, market_timeStr);
					BaseExcelUtil.cellBuild(rowNum, 8, sheet, style, Cell.CELL_TYPE_STRING, item.get("end_remark"));

					rowNum++;
	            } 

	          	       
	            DownLoadFileUtil.downloadExcel(request, response, sheet.getWorkbook(), fileName);

	           // result=ResultFactory.getResultListSuccess(items, page);
	        }catch(Exception ex){
	            ex.printStackTrace();
	           // result=ResultFactory.getResultFail();
	        }
	    }
	 
	 
	 public Map<String,String> changeStatus(User erpUser, Integer id,Byte status){
			Map<String,String> result=new HashMap<String,String>();
			try{
				this.log.info("用户" + erpUser.getRealName() + " 操作:" + id + " 操作类型:" + status );
				if(id==null || null==status){
					result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_FAIL);
					result.put(Constants.RETURN_MESSAGE_KEY, "数据异常");					
					return result;
				}

				
					UserNote record=new UserNote();
					record.setId(id);
					record.setStatus(status);							
					userNoteMapper.updateByPrimaryKeySelective(record);
				
				result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_SUCCESS);
				result.put(Constants.RETURN_MESSAGE_KEY, "success");		
				
				
			}catch(Exception ex){
				ex.printStackTrace();
				result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_FAIL);
				result.put(Constants.RETURN_MESSAGE_KEY, ex.getMessage());	
			}
			return result;
		}
	 
	 public Map<String,String> saveNote(User erpUser, Integer id,String remark){
			Map<String,String> result=new HashMap<String,String>();
			try{
				this.log.info("用户" + erpUser.getRealName() + " 操作:" + id + " 操作类型:" + remark );
				if(id==null || StringUtil.isBlank(remark)){
					result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_FAIL);
					result.put(Constants.RETURN_MESSAGE_KEY, "参数异常");					
					return result;
				}

				
					UserNote record=new UserNote();
					record.setId(id);
					record.setStatus((byte)1);	
					record.setEndRemark(remark);
					userNoteMapper.updateByPrimaryKeySelective(record);
				
				result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_SUCCESS);
				result.put(Constants.RETURN_MESSAGE_KEY, "success");		
				
				
			}catch(Exception ex){
				ex.printStackTrace();
				result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_FAIL);
				result.put(Constants.RETURN_MESSAGE_KEY, ex.getMessage());	
			}
			return result;
		}
	
	public void index1(NoteIndexReqVo model){
		 try{		
			 
			    model.setShopInfos(shopInfoDao.getAllShopInfoList());		 
			    PageHelper.startPage(model.getCpage(), model.getPageSize());
				List<Map> items=userNoteMapper.getUserNoteInfoByContion(
						model.getShopId(),1,
						model.getStatus(),
						model.getCustomerName(), model.getMobile());
					
				PageInfo<Map> pageInfo = new PageInfo<Map>(items);
				model.setTotalCount((int)pageInfo.getTotal());
				
				 for(Map item:items){
		            	//Long salePrice=null==item.get("sale_price")?null:Long.parseLong(item.get("sale_price").toString());
		            	//item.put("price",NumberUtil.forPriceView(salePrice));
		            	
		            }
				
				
				model.setItems(items);
			}catch(Exception ex){
				ex.printStackTrace();
				
			}
	}
	 public void exportXls1(
	    		HttpServletRequest request, 
				HttpServletResponse response,
				NoteIndexReqVo model){
	        try{
	        	
				String fileName ="投诉导出";

				String[] titleName = {"门店编码","门店名称","客户姓名","客户手机","投诉或问题说明","状态 ", "创建时间","处理意见"};			
				SXSSFSheet sheet = BaseExcelUtil.templateBuild(titleName, fileName);
		        CellStyle style = BaseExcelUtil.createGeneralCellStyle(sheet.getWorkbook());
		        int rowNum = 1;

		        List<Map> items=userNoteMapper.getUserNoteInfoByContion(
						model.getShopId(), 1,
						model.getStatus(),
						model.getCustomerName(), model.getMobile());
	            for(Map item:items){

	            	String status=item.get("status").toString();
	            	String statusName="";
	            	if(status.equals("0")){
	            		statusName="未处理 ";
	            	}
	            	if(status.equals("1")){
	            		statusName="已完成 ";
	            	}
	            	
	            	Date market_time = (Date)item.get("create_time");
					String market_timeStr = "";
					if(null != market_time){
						market_timeStr = DateUtil.format(market_time,"yyyy/MM/dd HH:mm:ss");
					}				

					BaseExcelUtil.cellBuild(rowNum, 0, sheet, style, Cell.CELL_TYPE_STRING, item.get("shop_code"));
	            	BaseExcelUtil.cellBuild(rowNum, 1, sheet, style, Cell.CELL_TYPE_STRING, item.get("shop_name"));
	            	BaseExcelUtil.cellBuild(rowNum, 2, sheet, style, Cell.CELL_TYPE_STRING, item.get("customer_name"));
					BaseExcelUtil.cellBuild(rowNum, 3, sheet, style, Cell.CELL_TYPE_STRING,item.get("mobile") );
					BaseExcelUtil.cellBuild(rowNum, 4, sheet, style, Cell.CELL_TYPE_STRING, item.get("remark"));
					BaseExcelUtil.cellBuild(rowNum, 5, sheet, style, Cell.CELL_TYPE_STRING, statusName);
					BaseExcelUtil.cellBuild(rowNum, 6, sheet, style, Cell.CELL_TYPE_STRING, market_timeStr);
					BaseExcelUtil.cellBuild(rowNum, 7, sheet, style, Cell.CELL_TYPE_STRING, item.get("end_remark"));

					rowNum++;
	            } 

	          	       
	            DownLoadFileUtil.downloadExcel(request, response, sheet.getWorkbook(), fileName);

	           // result=ResultFactory.getResultListSuccess(items, page);
	        }catch(Exception ex){
	            ex.printStackTrace();
	           // result=ResultFactory.getResultFail();
	        }
	    }
}
